//
//  YQBannerView.m
//  OverseasImmigration
//
//  Created by yuqin on 2019/6/2.
//  Copyright © 2019年 yuqin. All rights reserved.
//

#import "YQBannerView.h"
@interface YQBannerView ()<CWCarouselDatasource, CWCarouselDelegate>

@property (nonatomic, strong) NSMutableArray *imageArr;

@end
@implementation YQBannerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupBannerView];
    }
    return self;
}

- (UIView *)animationView{
    if(!_animationView) {
        _animationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 230)];
        [self addSubview:self.animationView];
    }
    return _animationView;
}

-(NSMutableArray *)imageArr
{
    if (!_imageArr) {
        _imageArr = [NSMutableArray arrayWithObjects:@"http://65.49.134.4:8081/icon/1.jpg",@"http://65.49.134.4:8081/icon/2.jpg",@"http://65.49.134.4:8081/icon/3.jpg", nil];
    }
    return _imageArr;
}

- (void)setupBannerView {
    self.animationView.backgroundColor = [UIColor whiteColor];
    CWFlowLayout *flowLayout = [[CWFlowLayout alloc] initWithStyle:CWCarouselStyle_H_3];
    CWCarousel *carousel = [[CWCarousel alloc] initWithFrame:self.animationView.bounds
                                                    delegate:self
                                                  datasource:self
                                                  flowLayout:flowLayout];
    [self.animationView addSubview:carousel];
    NSDictionary *dic = @{@"view" : carousel};
    [self.animationView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[view]-0-|"
                                                                               options:kNilOptions
                                                                               metrics:nil
                                                                                 views:dic]];
    [self.animationView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[view]-0-|"
                                                                               options:kNilOptions
                                                                               metrics:nil
                                                                                 views:dic]];
    
    
    carousel.isAuto = YES;
    carousel.autoTimInterval = 2;
    carousel.endless = YES;
    carousel.backgroundColor = [UIColor whiteColor];
    [carousel registerViewClass:[UICollectionViewCell class] identifier:@"cellId"];
    [carousel freshCarousel];
    self.carousel = carousel;
}

#pragma mark - Delegate
- (NSInteger)numbersForCarousel {
    return self.imageArr.count;
}
- (UICollectionViewCell *)viewForCarousel:(CWCarousel *)carousel indexPath:(NSIndexPath *)indexPath index:(NSInteger)index{
    UICollectionViewCell *cell = [carousel.carouselView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor cyanColor];
    UIImageView *imgView = [cell.contentView viewWithTag:666];
    if(!imgView) {
        imgView = [[UIImageView alloc] initWithFrame:cell.contentView.bounds];
        imgView.tag = 666;
        [imgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://65.49.134.4:8081/icon/%ld.jpg",index + 1]]];
        //        imgView.backgroundColor = [UIColor redColor];
        imgView.contentMode = UIViewContentModeScaleToFill;
        imgView.clipsToBounds = YES;
        [cell.contentView addSubview:imgView];
        cell.layer.masksToBounds = YES;
        cell.layer.cornerRadius = 8;
    }
    return cell;
}

- (void)CWCarousel:(CWCarousel *)carousel didSelectedAtIndex:(NSInteger)index {
    NSLog(@"...%ld...", (long)index);
}


- (void)CWCarousel:(CWCarousel *)carousel didStartScrollAtIndex:(NSInteger)index indexPathRow:(NSInteger)indexPathRow {
    NSLog(@"开始滑动: %ld", index);
}


- (void)CWCarousel:(CWCarousel *)carousel didEndScrollAtIndex:(NSInteger)index indexPathRow:(NSInteger)indexPathRow {
    NSLog(@"结束滑动: %ld", index);
}


@end
